package com.css.zfzx.sjcj.modules.classifycode.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.classifycode.repository.ZfzxClassifyCodeNativeRepository;
import com.css.zfzx.sjcj.modules.classifycode.repository.ZfzxClassifyCodeRepository;
import com.css.zfzx.sjcj.modules.classifycode.repository.entity.ZfzxClassifyCodeEntity;
import com.css.zfzx.sjcj.modules.classifycode.service.ZfzxClassifyCodeService;
import com.css.zfzx.sjcj.modules.classifycode.viewobjects.ZfzxClassifyCodeQueryParams;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @author tao
 * @date 2020-11-06
 */
@Service
public class ZfzxClassifyCodeServiceImpl implements ZfzxClassifyCodeService {

	@Autowired
    private ZfzxClassifyCodeRepository zfzxClassifyCodeRepository;
    @Autowired
    private ZfzxClassifyCodeNativeRepository zfzxClassifyCodeNativeRepository;

    @Override
    public JSONObject queryZfzxClassifyCodes(ZfzxClassifyCodeQueryParams queryParams, int curPage, int pageSize) {
        Page<ZfzxClassifyCodeEntity> zfzxClassifyCodePage = this.zfzxClassifyCodeNativeRepository.queryZfzxClassifyCodes(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(zfzxClassifyCodePage);

        String code = this.getClassifyCode("地震灾害","区划数据","潜在震源区分布图");
        code = this.getClassifyCode("地震灾害","区划数据","钻孔不同应变幅值对应剪切模量比与阻尼比的试验室测试结果");
        code = this.getClassifyCode("地震灾害","区划数据","地震动参数区划图");
        code = this.getClassifyCode("地震灾害","区划数据","123");
        code = this.getClassifyCode("地震灾害","重点隐患","钻孔不同应变幅值对应剪切模量比与阻尼比的试验室测试结果");
        return jsonObject;
    }


    @Override
    public ZfzxClassifyCodeEntity getZfzxClassifyCode(String id) {
        ZfzxClassifyCodeEntity zfzxClassifyCode = this.zfzxClassifyCodeRepository.findById(id).orElse(null);
         return zfzxClassifyCode;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZfzxClassifyCodeEntity saveZfzxClassifyCode(ZfzxClassifyCodeEntity zfzxClassifyCode) {
        String uuid = UUIDGenerator.getUUID();
        zfzxClassifyCode.setId(uuid);
        zfzxClassifyCode.setCreateUser(PlatformSessionUtils.getUserId());
        zfzxClassifyCode.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.zfzxClassifyCodeRepository.save(zfzxClassifyCode);
        return zfzxClassifyCode;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZfzxClassifyCodeEntity updateZfzxClassifyCode(ZfzxClassifyCodeEntity zfzxClassifyCode) {
        zfzxClassifyCode.setUpdateUser(PlatformSessionUtils.getUserId());
        zfzxClassifyCode.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.zfzxClassifyCodeRepository.save(zfzxClassifyCode);
        return zfzxClassifyCode;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteZfzxClassifyCodes(List<String> ids) {
        List<ZfzxClassifyCodeEntity> zfzxClassifyCodeList = this.zfzxClassifyCodeRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(zfzxClassifyCodeList) && zfzxClassifyCodeList.size() > 0) {
            for(ZfzxClassifyCodeEntity zfzxClassifyCode : zfzxClassifyCodeList) {
                this.zfzxClassifyCodeRepository.delete(zfzxClassifyCode);
            }
        }
    }

    @Override
    public String getClassifyCode(String firstClass,String secondClass,String thirdClass) {
        String codeName = firstClass + "-" + secondClass + "-" + thirdClass;
        String classifyCode = SingletonClassifyCode.getInstance().getClassifyCode(codeName);

        return classifyCode;
    }
}
